import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
Page({
  data: {
    // 当前的位置
    latitude: "",
    longitude: "",
    // 画圆
    circles: [],
    chanyeApiList: [], // 企业后台接口信息集合
    // markers: [], // 企业标记点集合
    markers: [],
    chanyeList: [
      {
        name: "电子信息",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/dzxx_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/dzxx_2.png",
      },
      {
        name: "数字经济",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/szjj_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/szjj_2.png",
      },
      {
        name: "高端装备制造",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/gszbzz_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/gszbzz_2.png",
      },
      {
        name: "生物医药",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/swyy_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/swyy_2.png",
      },
      {
        name: "集成电路",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/jcdl_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/jcdl_2.png",
      },
      {
        name: "汽车零部件",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/qclbj_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/qclbj_2.png",
      },
      {
        name: "先进材料",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/xjcl_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/xjcl_2.png",
      },
      {
        name: "软件服务",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/rjfw_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/rjfw_2.png",
      },
      {
        name: "航空航天",
        url1: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/hkht_1.png",
        url2: " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/hkht_2.png",
      },
    ],
    allChanyeList: [
      "电子信息",
      "数字经济",
      "高端装备制造",
      "生物医药",
      "集成电路",
      "汽车零部件",
      "先进材料",
      "软件服务",
      "航空航天",
    ],
    activeIndex: 0, // 默认的产业选择
    showTabName: "", // 顶部显示的产业名称
    mapCtx: null, // 地图实例对象
    totalQy: "", // 顶部数量
    topImg: "", // 顶部图标
  },

  // 页面解析完成后执行
  onReady: function () {
    // 步骤一、常见地图实例对象
    this.mapCtx = wx.createMapContext("hotMap");
  },
  onLoad: function (e) {
    const that = this;
    // 获取当前的位置信息
    getApp().globalData.getLocation({
      type: "gcj02",
      success(res) {
        that.setData({
          longitude: that.transMarker([res.longitude, res.latitude])[0],
          latitude: that.transMarker([res.latitude, res.latitude])[1],
          circles: [
            {
              latitude: res.latitude,
              longitude: res.longitude,
              color: "#eee",
              opacity: 0.2,
              fillColor: "#7cb5ec88",
              radius: 1000,
              strokeWidth: 2,
            },
          ],
        });
        // 数据初始化
        that.initInfos();
      },
    });
  },

  /**
   * methods
   */
  // 初始化数据
  async initInfos() {
    // 获取周边企业的坐标
    await this.getkeyIndustryCount();
    // 初始化markers;
    this.initMarkers();
  },

  // 获取产业的分布
  async getkeyIndustryCount() {
    this.setData({
      showTabName: this.data.allChanyeList[this.data.activeIndex],
      topImg: this.data.chanyeList[this.data.activeIndex].url1,
    });
    const params = {
      keyIndustry: this.data.allChanyeList[this.data.activeIndex] + "产业",
    };
    // console.log(params, "params");
    const { data } = await getApp().globalData.request(
      "/map/getkeyIndustryCount",
      params,
      "POST"
    );
    // 计算总和
    let totalNum = 0;
    data.forEach((pre) => (totalNum = totalNum + pre.quantity));
    console.log(data, "全部的产业分布");
    this.setData({
      chanyeApiList: data,
      totalQy: totalNum,
    });
  },

  // 初始化点标记
  initMarkers() {
    const markers = [];
    this.data.chanyeApiList.forEach((val, index) => {
      if (val.location) {
        markers.push({
          id: index,
          region: val.region,
          width: 0,
          height: 0,
          latitude: val.location.lat,
          longitude: val.location.lng,
          iconPath:
            " https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/mapicon.png",
          label: {
            id: index,
            content: `${val.region}\n${val.quantity}`,
            fontSize: 9,
            width: 50,
            height: 50,
            borderWidth: 2,
            borderColor: "#fff",
            bgColor: "#277EE8",
            color: "#fff",
            borderRadius: 50,
            textAlign: "center",
            anchorX: 0,
            anchorY: -18,
          },
        });
      }
    });
    this.setData({
      markers: markers,
    });
  },
  // 坐标系转化
  transMarker(item) {
    const resae = gcoord.transform(
      [item[0], item[1]],
      gcoord.GCJ02, // 当前坐标系
      gcoord.BD09 // 目标坐标系
    );
    return resae;
  },

  // 点击区域
  markerCalloutTap(e) {
    const region = this.data.chanyeApiList[e.markerId].region;
    const keyIndustry = this.data.allChanyeList[this.data.activeIndex] + "产业";
    wx.navigateTo({
      url: `../cyMap/cyMap?region=${region}&keyIndustry=${keyIndustry}&topImg=${this.data.topImg}`,
    });
  },

  // 点击事件
  clickCy(e) {
    console.log(e, "e");
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
    });
    this.initInfos();
  },
});
